import { getOrderInfo, updateRescueStatus } from "@/config/api/roadRescue"

// 复用代码
export default {
	data() {
		return{
			rescue: null,
			
			statusToObj:{
				"0": {
					name: "等待中",
					url: "/order/waiting",
				},
				"1": {
					name: "已接单",
					url: "/order/received",
				},
				"2": {
					name: "救援中",
					url: "/order/arrived",
				},
				"3": {
					name: "待支付",
					url: "/pay/index",
				},
				"4": {
					name: "待评价",
					url: "/pay/judge",
				},
				"5": {
					name: "已完成",
					url: "/pay/detail",
				},
				"6": {
					name: "已取消",
					url: "/pay/detail",
				},
			}
		}
	},
	methods: {
		timerPromise(sec) {
			return new Promise((res, rej) => {
				this.timer = setTimeout(() => {
					res()
				}, sec * 1000)
			})
		},

		getRescue(options, nowStatus = false) {
			const id = options.id
			if (!id) return uni.navigateTo({ url: "/pages/roadRescue/index" })

			return getOrderInfo(id)
				.then(res => {
					if (nowStatus !== false) {
						if (res.rescueStatus != nowStatus) {
							uni.navigateTo({ url: `/pages/roadRescue${this.statusToObj[res.rescueStatus].url}?id=${res.id}` })
						}
					}

					res.intersectionImg = uni.$u.http.config.baseURL + res.intersectionImg
					if (res.headPortrait) res.headPortrait = uni.$u.http.config.baseURL + res.headPortrait
					this.rescue = res;
				})
		},

		updateStatus(status) {
			if (!this.rescue) return new Promise((res, rej) => rej())

			return updateRescueStatus({
				id: this.rescue.id,
				rescueStatus: status
			})
		}
	}
}